package view;

import java.awt.Point;
import java.util.ArrayList;

public class PictureModel{
	private DrawPanel paint;
	private ArrayList<Block> blocks = new ArrayList<Block>();
	private ArrayList<Line> lines = new ArrayList<Line>();
	
	public PictureModel(DrawPanel paint){
		this.paint = paint;
	}
	
	public void draw(){
		for(Block b:blocks){
			b.draw(paint.getGraphics2D());
		}
		for(Line l:lines){
			l.draw(paint.getGraphics2D());
		}
		paint.repaint();
	}
	
	public void drawControlPoints(){
		for(Block b:blocks){
			b.drawControlPoints(paint.getGraphics2D());
			paint.repaint();
		}
	}
	
	public void drawZ(){
		for(Block b:blocks){
			b.drawZ(paint.getGraphics2D());
			paint.repaint();
		}
	}
	
	public void drawInfinityCycleAsRed(ArrayList<ArrayList<Integer>> cycles){
		for(ArrayList<Integer> cycle: cycles){
			drawCycleLines(cycle);
		}
	}
	private void drawCycleLines(ArrayList<Integer> cycle){
		if(cycle.size() == 1){
			getLine(cycle.get(0), cycle.get(0)).drawRed(paint.getGraphics2D());
			return;
		}else if(cycle.size() == 2){
			getLine(cycle.get(0), cycle.get(1)).drawRed(paint.getGraphics2D());
			getLine(cycle.get(1), cycle.get(0)).drawRed(paint.getGraphics2D());
			return;
		}
		for(int i = 1; i < cycle.size(); i++){
			getLine(cycle.get(i-1), cycle.get(i)).drawRed(paint.getGraphics2D());
		}
		getLine(cycle.get(cycle.size()-1), cycle.get(0)).drawRed(paint.getGraphics2D());
		
	}
	
	
	public Block getBlockByPoint(Point point){
		for(Block b:blocks){
			if(b.contains(point))
				return b;
		}
		return null;
	}
	
	public Line getLineByPoint(Point point){
		for(Line l:lines){
			if(l.contains(point)){
				return l;
			}
		}
		return null;
	}
	
	public Block getBlockByControlPoint(Point point){
		for(Block b:blocks){
			if(b.haveControlPoint(point))
				return b;
		}
		return null;
	}
	
	public void removeBlock(Block block){
		block.cleanDraw(paint.getGraphics2D());
		blocks.remove(block);
		draw();
	}
	
	public void removeLine(Line line){
		lines.remove(line);
		paint.clear();
		draw();
	}
	public void removeBlockWithLine(Block block){
		ArrayList<Line> blockLines = getLinesByBlock(block);
		for(Line l:blockLines){
			l.cleanDraw(paint.getGraphics2D());
			lines.remove(l);
		}
		removeBlock(block);
	}
	private ArrayList<Line> getLinesByBlock(Block block){
		ArrayList<Line> blockLines = new ArrayList<Line>();
		for(Line line:lines){
			if(line.getCauseBlock().equals(block) || line.getSecondBlock().equals(block)){
				blockLines.add(line);
			}
		}
		return blockLines;
	}
	
	public boolean hasStartEnd(){
		int count = 0;
		for(Block block: blocks){
			if(block.getId() == -1 || block.getId() == -2)
				count++;
		}
		if(count == 2)
			return true;
		else
			return false;
	}
	
	public void addBlock(Block block){
		blocks.add(block);
	}
	public void addLine(Line line){
		lines.add(line);
	}
	public ArrayList<Block> getBlocks() {
		return blocks;
	}

	public void setBlocks(ArrayList<Block> blocks) {
		this.blocks = blocks;
	}

	public ArrayList<Line> getLines() {
		return lines;
	}
	public Line getLine(int n1, int n2){
		for(Line line: lines){
			if(line.getCauseBlock().getZId() == n1 && line.getSecondBlock().getZId() == n2){
				return line;
			}
		}
		return null;
	}

	public void setLines(ArrayList<Line> lines) {
		this.lines = lines;
	}
}
